package com.techysky.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.techysky.entity.DataModule;
import com.techysky.entity.PmDm;
import com.techysky.entity.PublicModule;
import com.techysky.repository.DataModuleRepository;
import com.techysky.repository.PmDmRepository;
import com.techysky.repository.PublicModuleRepository;
import com.techysky.service.PublicModuleService;

/**
 * 
 * @author zhangxu
 *
 */
@Service("publicModuleService")
public class PublicModuleServiceImpl implements PublicModuleService {

	private static final long serialVersionUID = 1L;
	@Autowired
	private PmDmRepository pmDmRepository;
	@Autowired
	private PublicModuleRepository publicModuleRepository;
	
	@Autowired
	private transient DataModuleRepository dataModuleRepository;
	
	@Override
	public Map<String, Object> findByParentIdAndType(String type, String pid) {
		Map<String,Object> result = new HashMap<String,Object>();
		if("pm".equals(type)) {
			result.put("pms", this.publicModuleRepository.findByParentId(pid));
		}else if("dm".equals(type)) {
			result.put("dms", this.pmDmRepository.findByPmId(pid));
		}else if("pds".equals(type)) {
			List<Map<String,Object>> pmss = new ArrayList<>();
			List<PmDm> pds= this.pmDmRepository.findByPmId(pid);
			if(!pds.isEmpty()) {
				pds.forEach(pd->{
					Map<String,Object> map = new HashMap<>();
					map.put("id", pd.getDmId());
					map.put("title", pd.getDmTitle());
					map.put("isDm", true);
					map.put("orderCode", pd.getOrderCode());
					pmss.add(map);
				});
			}
			List<PublicModule> pms= this.publicModuleRepository.findByParentId(pid);
			if(!pms.isEmpty()) {
				pms.forEach(pm->{
					Map<String,Object> map = new HashMap<>();
					map.put("id", pm.getId());
					map.put("title", pm.getTitle());
					map.put("isDm", false);
					map.put("orderCode", "-1");
					pmss.add(map);
				});
			}
			result.put("pds", pmss);
		}
		return result;
	}
	
	@Override
	public Map<String,Object>  findByParentId(String pid) {
		Map<String,Object> result = new HashMap<String,Object>();
		List<PublicModule> pms= this.publicModuleRepository.findByParentId(pid);
		result.put("pms", pms);
		List<PmDm> pmdms= this.pmDmRepository.findByPmId(pid);
		result.put("dms", pmdms);
		/*if(StringUtils.isEmpty(pid)) {//当pid为空查询的是一级菜单
			List<PublicModule> pms=this.publicModuleRepository.findByParentIdIsNull();
			result.put("root",pms);
		}else {//当pid不为查询出来的就是文件夹和文件列表
			List<PublicModule> pms= this.publicModuleRepository.findByParentId(pid);//此处查出的是文件夹
			List<PmDm> pmdms= this.pmDmRepository.findByPmId(pid);//此处查询出来的是文件列表
		}*/
		return result;
	}
	@Override
	public List<PublicModule> findRootElement(String id) {
		return this.publicModuleRepository.findByProjectIdAndParentIdIsNull(id);
	}
	public int getChildren(String parentid) {
		List<PublicModule> pmList=this.publicModuleRepository.findByParentId(parentid);
		int num=0;
		if(!pmList.isEmpty()){
			return pmList.size();
		}
		else
			return num;
	}
	public int getChildren2(String parentid) {
		List<DataModule> dmList=this.dataModuleRepository.findByPublicModuleId(parentid);
		int num=0;
		if(!dmList.isEmpty()){
			return dmList.size();
		}
		else
			return num;
	}
	@Override
	public PublicModule saveOrUpdate(PublicModule publicModule) {
		return this.publicModuleRepository.saveAndFlush(publicModule);
	}
	@Override
	public PublicModule getObject(String publicModuleId) {
		return this.publicModuleRepository.findOne(publicModuleId);
	}
	@Override
	public List<Map<String,Object>> findByParentIdJql(String parentId){
		if(null==parentId){
			return this.publicModuleRepository.findByParentIdJqlNull();
		}
		else{
			return this.publicModuleRepository.findByParentIdJql(parentId);
		}
	}

}
